package Views;

import Controllers.*;
import Models.*;
import Views.TableModels.*;
import byggemarked.Tools;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import java.util.ArrayList;
import java.util.Date;
import javax.swing.JOptionPane;
import javax.swing.JTable;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;

/**
 * @author Tanja og Morten
 */
public class SaleView extends javax.swing.JFrame implements ListSelectionListener, WindowListener
{
	private SaleCTR controller = new SaleCTR();
	private Sale currentSaleTemp = null;
	private Employee currentEmployee = null;
	private Customer saleCustomerTemp = null;
	private Kitchen saleKitchenTemp = null;
	private ArrayList<Item> saleItemsTemp = new ArrayList<Item>();
	private CustomerPicker cp = null;
	private EmployeePicker ep = null;
	private KitchenPicker kp = null;

    public SaleView(Employee employee)
	{
		this.currentEmployee = employee;
        initComponents();
		addSaleTable.getSelectionModel().addListSelectionListener(this);
		findTable.getSelectionModel().addListSelectionListener(this);
		this.startNewSale();
		findTable.addMouseListener(new MouseAdapter(){
			@Override
			public void mouseClicked(MouseEvent e){
				if (e.getClickCount() == 2)
				{
					if(findTable.getSelectedRowCount() == 1)
					{
						int chosenId = Integer.parseInt(findTable.getValueAt(findTable.getSelectedRow(), 0).toString());
						new SaleInfoView(controller.getSale(chosenId)).setVisible(true);
					}
				}
			}
		 });
    }

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        jTable1 = new javax.swing.JTable();
        deliveryBG = new javax.swing.ButtonGroup();
        deliveredBG = new javax.swing.ButtonGroup();
        payedBG = new javax.swing.ButtonGroup();
        jTabbedPane1 = new javax.swing.JTabbedPane();
        addSalePanel = new javax.swing.JPanel();
        addSaleLabel = new javax.swing.JLabel();
        newSaleButton = new javax.swing.JButton();
        addCustomerLabel = new javax.swing.JLabel();
        addCustomerTF = new javax.swing.JTextField();
        addCustomerButton = new javax.swing.JButton();
        addCashSaleCB = new javax.swing.JCheckBox();
        addDeliveryLabel = new javax.swing.JLabel();
        deliveryYesRB = new javax.swing.JRadioButton();
        deliveryNoRB = new javax.swing.JRadioButton();
        addDeliveredLabel = new javax.swing.JLabel();
        deliveredYesRB = new javax.swing.JRadioButton();
        deliveredNoRB = new javax.swing.JRadioButton();
        addKitchenLabel = new javax.swing.JLabel();
        addKitchenTF = new javax.swing.JTextField();
        addKitchenButton = new javax.swing.JButton();
        addItemLabel = new javax.swing.JLabel();
        addItemSP = new javax.swing.JSpinner();
        addAddItemButton = new javax.swing.JButton();
        addRemoveItemButton = new javax.swing.JButton();
        jScrollPane2 = new javax.swing.JScrollPane();
        addSaleTable = new javax.swing.JTable();
        addPriceLabel = new javax.swing.JLabel();
        addPriceTF = new javax.swing.JTextField();
        addPriceKrLabel = new javax.swing.JLabel();
        addAddButton = new javax.swing.JButton();
        addPayButton = new javax.swing.JButton();
        findSalePanel = new javax.swing.JPanel();
        findSaleLabel = new javax.swing.JLabel();
        findEmployeeLabel = new javax.swing.JLabel();
        findEmployeeTF = new javax.swing.JTextField();
        findEmployeeButton = new javax.swing.JButton();
        findCustomerLabel = new javax.swing.JLabel();
        findCustomerTF = new javax.swing.JTextField();
        findCustomerButton = new javax.swing.JButton();
        findMinimumPriceLabel = new javax.swing.JLabel();
        findMinimumPriceSP = new javax.swing.JSpinner();
        findMaximumPriceLabel = new javax.swing.JLabel();
        findMaximumPriceSP = new javax.swing.JSpinner();
        findSaleDateLabel = new javax.swing.JLabel();
        findFromDateLabel = new javax.swing.JLabel();
        findSaleDateFromTF = new javax.swing.JTextField();
        findToDateLabel = new javax.swing.JLabel();
        findSaleDateToTF = new javax.swing.JTextField();
        findPaidLabel = new javax.swing.JLabel();
        findPaidYesRB = new javax.swing.JRadioButton();
        findPaidNoRB = new javax.swing.JRadioButton();
        findButton = new javax.swing.JButton();
        jScrollPane3 = new javax.swing.JScrollPane();
        findTable = new javax.swing.JTable();
        findShowAllButton = new javax.swing.JButton();
        getSalePanel = new javax.swing.JPanel();
        getSaleLabel = new javax.swing.JLabel();
        getIdNoLabel = new javax.swing.JLabel();
        getIdSP = new javax.swing.JSpinner();
        getSaleIdButton = new javax.swing.JButton();

        jTable1.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        jScrollPane1.setViewportView(jTable1);

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Salg");

        addSaleLabel.setText("Her kan tilføjes et nyt salg");

        newSaleButton.setText("Nyt salg");
        newSaleButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                newSaleButtonActionPerformed(evt);
            }
        });

        addCustomerLabel.setText("Kunde:");

        addCustomerTF.setEnabled(false);

        addCustomerButton.setText("Find");
        addCustomerButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                addCustomerButtonActionPerformed(evt);
            }
        });

        addCashSaleCB.setText("Kontantsalg");
        addCashSaleCB.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                addCashSaleCBActionPerformed(evt);
            }
        });

        addDeliveryLabel.setText("Levering:");

        deliveryBG.add(deliveryYesRB);
        deliveryYesRB.setText("Ja");

        deliveryBG.add(deliveryNoRB);
        deliveryNoRB.setSelected(true);
        deliveryNoRB.setText("Nej");

        addDeliveredLabel.setText("Leveret:");

        deliveredBG.add(deliveredYesRB);
        deliveredYesRB.setSelected(true);
        deliveredYesRB.setText("Ja");

        deliveredBG.add(deliveredNoRB);
        deliveredNoRB.setText("Nej");

        addKitchenLabel.setText("Køkken:");

        addKitchenTF.setEnabled(false);

        addKitchenButton.setText("Find");
        addKitchenButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                addKitchenButtonActionPerformed(evt);
            }
        });

        addItemLabel.setText("Tilføj vare:");

        addAddItemButton.setText("Tilføj");
        addAddItemButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                addAddItemButtonActionPerformed(evt);
            }
        });

        addRemoveItemButton.setText("Fjern");
        addRemoveItemButton.setEnabled(false);
        addRemoveItemButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                addRemoveItemButtonActionPerformed(evt);
            }
        });

        addSaleTable.setModel(new ItemTableModel());
        jScrollPane2.setViewportView(addSaleTable);

        addPriceLabel.setText("Pris:");

        addPriceTF.setEnabled(false);

        addPriceKrLabel.setText("kr.");

        addAddButton.setText("Afslut salg");
        addAddButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                addAddButtonActionPerformed(evt);
            }
        });

        addPayButton.setText("Betal");
        addPayButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                addPayButtonActionPerformed(evt);
            }
        });

        org.jdesktop.layout.GroupLayout addSalePanelLayout = new org.jdesktop.layout.GroupLayout(addSalePanel);
        addSalePanel.setLayout(addSalePanelLayout);
        addSalePanelLayout.setHorizontalGroup(
            addSalePanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(addSalePanelLayout.createSequentialGroup()
                .addContainerGap()
                .add(addSalePanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(addDeliveryLabel)
                    .add(addSalePanelLayout.createSequentialGroup()
                        .add(addSaleLabel)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED, 207, Short.MAX_VALUE)
                        .add(newSaleButton))
                    .add(org.jdesktop.layout.GroupLayout.TRAILING, addSalePanelLayout.createSequentialGroup()
                        .add(addPriceLabel)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(addPriceTF, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 87, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(addPriceKrLabel)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED, 132, Short.MAX_VALUE)
                        .add(addAddButton)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.UNRELATED)
                        .add(addPayButton))
                    .add(org.jdesktop.layout.GroupLayout.TRAILING, jScrollPane2, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .add(org.jdesktop.layout.GroupLayout.TRAILING, addSalePanelLayout.createSequentialGroup()
                        .add(addSalePanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                            .add(addItemLabel)
                            .add(addCustomerLabel)
                            .add(addDeliveredLabel)
                            .add(addKitchenLabel))
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED, 30, Short.MAX_VALUE)
                        .add(addSalePanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                            .add(addSalePanelLayout.createSequentialGroup()
                                .add(deliveredYesRB)
                                .add(18, 18, 18)
                                .add(deliveredNoRB))
                            .add(org.jdesktop.layout.GroupLayout.TRAILING, addSalePanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                                .add(addSalePanelLayout.createSequentialGroup()
                                    .add(deliveryYesRB)
                                    .add(18, 18, 18)
                                    .add(deliveryNoRB))
                                .add(org.jdesktop.layout.GroupLayout.TRAILING, addSalePanelLayout.createSequentialGroup()
                                    .add(addItemSP, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 212, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                                    .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                                    .add(addAddItemButton)
                                    .addPreferredGap(org.jdesktop.layout.LayoutStyle.UNRELATED)
                                    .add(addRemoveItemButton))
                                .add(addSalePanelLayout.createSequentialGroup()
                                    .add(addSalePanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING, false)
                                        .add(addKitchenTF)
                                        .add(addCustomerTF, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 136, Short.MAX_VALUE))
                                    .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                                    .add(addSalePanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                                        .add(addSalePanelLayout.createSequentialGroup()
                                            .add(addCustomerButton)
                                            .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                                            .add(addCashSaleCB))
                                        .add(addKitchenButton)))))))
                .addContainerGap(org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        addSalePanelLayout.setVerticalGroup(
            addSalePanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(addSalePanelLayout.createSequentialGroup()
                .add(addSalePanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(addSalePanelLayout.createSequentialGroup()
                        .addContainerGap()
                        .add(addSaleLabel))
                    .add(newSaleButton))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(addSalePanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(addCustomerLabel)
                    .add(addCustomerButton)
                    .add(addCashSaleCB)
                    .add(addCustomerTF, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(addSalePanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(addDeliveryLabel)
                    .add(deliveryYesRB)
                    .add(deliveryNoRB))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.UNRELATED)
                .add(addSalePanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(addDeliveredLabel)
                    .add(deliveredYesRB)
                    .add(deliveredNoRB))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(addSalePanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(addKitchenLabel)
                    .add(addKitchenTF, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .add(addKitchenButton))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(addSalePanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(addRemoveItemButton)
                    .add(addAddItemButton)
                    .add(addItemSP, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .add(addItemLabel))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(jScrollPane2, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 144, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(addSalePanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(addPayButton)
                    .add(addAddButton)
                    .add(addPriceLabel)
                    .add(addPriceTF, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .add(addPriceKrLabel))
                .addContainerGap(26, Short.MAX_VALUE))
        );

        jTabbedPane1.addTab("Tilføj", addSalePanel);

        findSaleLabel.setText("Find et allerede afsluttet salg ved at søge på et eller flere kriterier");

        findEmployeeLabel.setText("Ansat:");

        findEmployeeTF.setEnabled(false);

        findEmployeeButton.setText("Find");
        findEmployeeButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                findEmployeeButtonActionPerformed(evt);
            }
        });

        findCustomerLabel.setText("Kunde:");

        findCustomerTF.setEnabled(false);

        findCustomerButton.setText("Find");
        findCustomerButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                findCustomerButtonActionPerformed(evt);
            }
        });

        findMinimumPriceLabel.setText("Minimumspris:");

        findMaximumPriceLabel.setText("Maximumspris:");

        findSaleDateLabel.setText("Salgsdato:");

        findFromDateLabel.setText("Fra:");

        findToDateLabel.setText("Til:");

        findPaidLabel.setText("Betalt:");

        payedBG.add(findPaidYesRB);
        findPaidYesRB.setText("Ja");

        payedBG.add(findPaidNoRB);
        findPaidNoRB.setText("Nej");

        findButton.setText("Søg");
        findButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                findButtonActionPerformed(evt);
            }
        });

        findTable.setModel(new SaleTableModel());
        jScrollPane3.setViewportView(findTable);

        findShowAllButton.setText("Vis alle");
        findShowAllButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                findShowAllButtonActionPerformed(evt);
            }
        });

        org.jdesktop.layout.GroupLayout findSalePanelLayout = new org.jdesktop.layout.GroupLayout(findSalePanel);
        findSalePanel.setLayout(findSalePanelLayout);
        findSalePanelLayout.setHorizontalGroup(
            findSalePanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(findSalePanelLayout.createSequentialGroup()
                .addContainerGap()
                .add(findSalePanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(findSalePanelLayout.createSequentialGroup()
                        .add(jScrollPane3, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 470, Short.MAX_VALUE)
                        .addContainerGap())
                    .add(findSalePanelLayout.createSequentialGroup()
                        .add(findSalePanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                            .add(findSaleLabel)
                            .add(findSalePanelLayout.createSequentialGroup()
                                .add(findSalePanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                                    .add(findSalePanelLayout.createSequentialGroup()
                                        .add(findSaleDateLabel)
                                        .add(18, 18, 18)
                                        .add(findFromDateLabel)
                                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                                        .add(findSaleDateFromTF, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 99, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                                    .add(findSalePanelLayout.createSequentialGroup()
                                        .add(findMinimumPriceLabel)
                                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                                        .add(findMinimumPriceSP, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 89, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)))
                                .add(11, 11, 11)
                                .add(findSalePanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                                    .add(findSalePanelLayout.createSequentialGroup()
                                        .add(findMaximumPriceLabel)
                                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                                        .add(findMaximumPriceSP, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 89, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                                    .add(findSalePanelLayout.createSequentialGroup()
                                        .add(findToDateLabel)
                                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                                        .add(findSaleDateToTF, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 105, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))))
                            .add(findSalePanelLayout.createSequentialGroup()
                                .add(findSalePanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING, false)
                                    .add(org.jdesktop.layout.GroupLayout.LEADING, findSalePanelLayout.createSequentialGroup()
                                        .add(findCustomerLabel)
                                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.UNRELATED)
                                        .add(findCustomerTF))
                                    .add(org.jdesktop.layout.GroupLayout.LEADING, findSalePanelLayout.createSequentialGroup()
                                        .add(findEmployeeLabel)
                                        .add(18, 18, 18)
                                        .add(findEmployeeTF, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 170, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)))
                                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                                .add(findSalePanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                                    .add(findCustomerButton)
                                    .add(findEmployeeButton)))
                            .add(findSalePanelLayout.createSequentialGroup()
                                .add(findPaidLabel)
                                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                                .add(findPaidYesRB)
                                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                                .add(findPaidNoRB))
                            .add(findSalePanelLayout.createSequentialGroup()
                                .add(findButton)
                                .addPreferredGap(org.jdesktop.layout.LayoutStyle.UNRELATED)
                                .add(findShowAllButton)))
                        .add(48, 48, 48))))
        );
        findSalePanelLayout.setVerticalGroup(
            findSalePanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(findSalePanelLayout.createSequentialGroup()
                .addContainerGap()
                .add(findSaleLabel)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.UNRELATED)
                .add(findSalePanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(findEmployeeLabel)
                    .add(findEmployeeTF, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .add(findEmployeeButton))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(findSalePanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(findCustomerLabel)
                    .add(findCustomerTF, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .add(findCustomerButton))
                .add(18, 18, 18)
                .add(findSalePanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(findMinimumPriceSP, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .add(findMaximumPriceLabel)
                    .add(findMaximumPriceSP, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .add(findMinimumPriceLabel))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(findSalePanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(findSaleDateLabel)
                    .add(findFromDateLabel)
                    .add(findSaleDateToTF, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .add(findSaleDateFromTF, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .add(findToDateLabel))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(findSalePanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(findPaidLabel)
                    .add(findPaidYesRB)
                    .add(findPaidNoRB))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(findSalePanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(findButton)
                    .add(findShowAllButton))
                .add(1, 1, 1)
                .add(jScrollPane3, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 141, Short.MAX_VALUE)
                .addContainerGap())
        );

        jTabbedPane1.addTab("Søg", findSalePanel);

        getSaleLabel.setText("Her kan der findes et salg ud fra et salgsID");

        getIdNoLabel.setText("ID:");

        getSaleIdButton.setText("Hent");
        getSaleIdButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                getSaleIdButtonActionPerformed(evt);
            }
        });

        org.jdesktop.layout.GroupLayout getSalePanelLayout = new org.jdesktop.layout.GroupLayout(getSalePanel);
        getSalePanel.setLayout(getSalePanelLayout);
        getSalePanelLayout.setHorizontalGroup(
            getSalePanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(getSalePanelLayout.createSequentialGroup()
                .addContainerGap()
                .add(getSalePanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(getSaleLabel)
                    .add(getSalePanelLayout.createSequentialGroup()
                        .add(getIdNoLabel)
                        .add(18, 18, 18)
                        .add(getIdSP, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 66, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                    .add(getSaleIdButton))
                .addContainerGap(220, Short.MAX_VALUE))
        );
        getSalePanelLayout.setVerticalGroup(
            getSalePanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(getSalePanelLayout.createSequentialGroup()
                .addContainerGap()
                .add(getSaleLabel)
                .add(18, 18, 18)
                .add(getSalePanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(getIdNoLabel)
                    .add(getIdSP, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .add(18, 18, 18)
                .add(getSaleIdButton)
                .addContainerGap(293, Short.MAX_VALUE))
        );

        jTabbedPane1.addTab("Hent", getSalePanel);

        org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(jTabbedPane1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 531, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(jTabbedPane1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 468, Short.MAX_VALUE)
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

	private void addCashSaleCBActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_addCashSaleCBActionPerformed
		if(addCashSaleCB.isSelected())
		{
			addCustomerButton.setEnabled(false);
			addCustomerTF.setText("");
		}
		else
		{
			addCustomerButton.setEnabled(true);
		}
}//GEN-LAST:event_addCashSaleCBActionPerformed

	private void addRemoveItemButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_addRemoveItemButtonActionPerformed
		int chosenItemNo = Integer.parseInt(addSaleTable.getValueAt(addSaleTable.getSelectedRow(), 0).toString());
		ItemCTR stockController = new ItemCTR();
		this.saleItemsTemp.remove(stockController.getItem(chosenItemNo));
		addSaleTable.setModel(new ItemTableModel(this.saleItemsTemp));
		this.calculateTotalPrice();
}//GEN-LAST:event_addRemoveItemButtonActionPerformed

	private void addCustomerButtonActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_addCustomerButtonActionPerformed
	{//GEN-HEADEREND:event_addCustomerButtonActionPerformed
		cp = new CustomerPicker();
		cp.addWindowListener(this);
		cp.setVisible(true);
	}//GEN-LAST:event_addCustomerButtonActionPerformed

	private void addKitchenButtonActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_addKitchenButtonActionPerformed
	{//GEN-HEADEREND:event_addKitchenButtonActionPerformed
		kp = new KitchenPicker();
		kp.addWindowListener(this);
		kp.setVisible(true);
	}//GEN-LAST:event_addKitchenButtonActionPerformed

	private void addAddItemButtonActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_addAddItemButtonActionPerformed
	{//GEN-HEADEREND:event_addAddItemButtonActionPerformed
		Controllers.ItemCTR stockController = new Controllers.ItemCTR();
		Item newItem = stockController.getItem(Integer.parseInt(addItemSP.getValue().toString()));
		if(newItem != null)
		{
			this.saleItemsTemp.add(newItem);
			this.calculateTotalPrice();
			addSaleTable.setModel(new ItemTableModel(this.saleItemsTemp));
		}
		else
		{
			JOptionPane.showMessageDialog(this, "Der findes ingen varer med det indtastede varenummer.", "Ugyldigt varenummer", JOptionPane.ERROR_MESSAGE);
		}
	}//GEN-LAST:event_addAddItemButtonActionPerformed

	private void addPayButtonActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_addPayButtonActionPerformed
	{//GEN-HEADEREND:event_addPayButtonActionPerformed
		JOptionPane.showMessageDialog(this, "Kunden skal betale "+addPriceTF.getText()+" kroner.", "Betaling af salg", JOptionPane.INFORMATION_MESSAGE);
		this.currentSaleTemp.setPaid(true);
		addPayButton.setEnabled(false);
	}//GEN-LAST:event_addPayButtonActionPerformed

	private void findButtonActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_findButtonActionPerformed
	{//GEN-HEADEREND:event_findButtonActionPerformed
		Double minPrice = Double.parseDouble(findMinimumPriceSP.getValue().toString());
		Double maxPrice = Double.parseDouble(findMaximumPriceSP.getValue().toString());
		Date startDate = null;
		try
		{
			if(!findSaleDateFromTF.getText().equals(""))
			{
				startDate = Tools.makeDateFromString(findSaleDateFromTF.getText());
			}
		}
		catch(NumberFormatException e)
		{
			JOptionPane.showMessageDialog(this, "Den første salgsdato er skrevet i et forkert format.", "Forkert datoformat", JOptionPane.WARNING_MESSAGE);
		}
		Date endDate = null;
		try
		{
			if(!findSaleDateToTF.getText().equals(""))
			{
				endDate = Tools.makeDateFromString(findSaleDateToTF.getText());
			}
		}
		catch(NumberFormatException e)
		{
			JOptionPane.showMessageDialog(this, "Den sidste salgsdato er skrevet i et forkert format.", "Forkert datoformat", JOptionPane.WARNING_MESSAGE);
		}
		boolean paid = true;
		if(findPaidYesRB.isSelected())
		{ paid = true; }
		else
		{ paid = false; }
		findTable.setModel(
				new SaleTableModel(
					this.controller.findSale(minPrice, maxPrice, startDate, endDate, saleCustomerTemp, currentEmployee, false, false, paid)));
	}//GEN-LAST:event_findButtonActionPerformed

	private void getSaleIdButtonActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_getSaleIdButtonActionPerformed
	{//GEN-HEADEREND:event_getSaleIdButtonActionPerformed
		Sale sale = this.controller.getSale(Integer.parseInt(getIdSP.getValue().toString()));
		if(sale != null)
		{
			new SaleInfoView(sale).setVisible(true);
		}
		else
		{
			JOptionPane.showMessageDialog(this, "Der er angivet et forkert ID.", "Forkert ID", JOptionPane.ERROR_MESSAGE);
		}
	}//GEN-LAST:event_getSaleIdButtonActionPerformed

	private void findEmployeeButtonActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_findEmployeeButtonActionPerformed
	{//GEN-HEADEREND:event_findEmployeeButtonActionPerformed
		ep = new EmployeePicker();
		ep.addWindowListener(this);
		ep.setVisible(true);
	}//GEN-LAST:event_findEmployeeButtonActionPerformed

	private void findCustomerButtonActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_findCustomerButtonActionPerformed
	{//GEN-HEADEREND:event_findCustomerButtonActionPerformed
		cp = new CustomerPicker();
		cp.addWindowListener(this);
		cp.setVisible(true);
	}//GEN-LAST:event_findCustomerButtonActionPerformed

	private void addAddButtonActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_addAddButtonActionPerformed
	{//GEN-HEADEREND:event_addAddButtonActionPerformed
		Sale newSale = this.controller.addSale(saleCustomerTemp, currentEmployee);
		for (Item item : saleItemsTemp)
		{
			newSale.addItem(item);
		}
		boolean delivery = true;
		if(deliveryYesRB.isSelected())
		{
			delivery = true;
		}
		else
		{
			delivery = false;
		}
		newSale.setDelivery(delivery);
		boolean delivered = true;
		if(deliveredYesRB.isSelected())
		{
			delivered = true;
		}
		else
		{
			delivered = false;
		}
		newSale.setDelivered(delivered);
		this.currentSaleTemp = newSale;
		addAddButton.setEnabled(false);
		addPayButton.setEnabled(true);
	}//GEN-LAST:event_addAddButtonActionPerformed

	private void newSaleButtonActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_newSaleButtonActionPerformed
	{//GEN-HEADEREND:event_newSaleButtonActionPerformed
		this.startNewSale();
	}//GEN-LAST:event_newSaleButtonActionPerformed

	private void findShowAllButtonActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_findShowAllButtonActionPerformed
	{//GEN-HEADEREND:event_findShowAllButtonActionPerformed
		findTable.setModel(new SaleTableModel(this.controller.getAllSales()));
	}//GEN-LAST:event_findShowAllButtonActionPerformed

	private void startNewSale()
	{
		this.currentSaleTemp = null;
		addCustomerTF.setText("");
		findCustomerTF.setText("");
		this.saleCustomerTemp = null;
		addCashSaleCB.setSelected(false);
		deliveryNoRB.setSelected(true);
		deliveredYesRB.setSelected(true);
		addKitchenTF.setText("");
		this.saleKitchenTemp = null;
		addItemSP.setValue(0);
		this.saleItemsTemp = new ArrayList<Item>();
		addSaleTable.setModel(new ItemTableModel());
		addAddButton.setEnabled(true);
		addPayButton.setEnabled(false);
	}

	private void calculateTotalPrice()
	{
		double totalPrice = 0;
		for (Item item : saleItemsTemp) {
			totalPrice += item.getPrice();
		}
		addPriceTF.setText(String.valueOf(totalPrice));
	}

	public void valueChanged(ListSelectionEvent e)
	{
		if(addSaleTable.getSelectedRowCount() == 1)
		{
			addRemoveItemButton.setEnabled(true);
		}
		else
		{
			addRemoveItemButton.setEnabled(false);
		}
	}

	public void windowClosing(WindowEvent e)
	{
		if(ep != null)
		{
			this.currentEmployee = ep.getChosenEmployee();
			if(this.currentEmployee != null)
			{
				findEmployeeTF.setText(this.currentEmployee.getName());
			}
			ep = null;
		}
		else if(cp != null)
		{
			this.saleCustomerTemp = cp.getChosenCustomer();
			if(this.saleCustomerTemp != null)
			{
				addCustomerTF.setText(byggemarked.Tools.getNameOrCompanyName(this.saleCustomerTemp));
				findCustomerTF.setText(byggemarked.Tools.getNameOrCompanyName(this.saleCustomerTemp));
			}
			cp = null;
		}
		else if(kp != null)
		{
			this.saleKitchenTemp = kp.getChosenKitchen();
			if(this.saleKitchenTemp != null)
			{
				addKitchenTF.setText(this.saleKitchenTemp.getName());
			}
			kp = null;
		}
	}

	public void windowOpened(WindowEvent e) {}
	public void windowClosed(WindowEvent e) {}
	public void windowIconified(WindowEvent e) {}
	public void windowDeiconified(WindowEvent e) {}
	public void windowActivated(WindowEvent e) {}
	public void windowDeactivated(WindowEvent e) {}

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton addAddButton;
    private javax.swing.JButton addAddItemButton;
    private javax.swing.JCheckBox addCashSaleCB;
    private javax.swing.JButton addCustomerButton;
    private javax.swing.JLabel addCustomerLabel;
    private javax.swing.JTextField addCustomerTF;
    private javax.swing.JLabel addDeliveredLabel;
    private javax.swing.JLabel addDeliveryLabel;
    private javax.swing.JLabel addItemLabel;
    private javax.swing.JSpinner addItemSP;
    private javax.swing.JButton addKitchenButton;
    private javax.swing.JLabel addKitchenLabel;
    private javax.swing.JTextField addKitchenTF;
    private javax.swing.JButton addPayButton;
    private javax.swing.JLabel addPriceKrLabel;
    private javax.swing.JLabel addPriceLabel;
    private javax.swing.JTextField addPriceTF;
    private javax.swing.JButton addRemoveItemButton;
    private javax.swing.JLabel addSaleLabel;
    private javax.swing.JPanel addSalePanel;
    private javax.swing.JTable addSaleTable;
    private javax.swing.ButtonGroup deliveredBG;
    private javax.swing.JRadioButton deliveredNoRB;
    private javax.swing.JRadioButton deliveredYesRB;
    private javax.swing.ButtonGroup deliveryBG;
    private javax.swing.JRadioButton deliveryNoRB;
    private javax.swing.JRadioButton deliveryYesRB;
    private javax.swing.JButton findButton;
    private javax.swing.JButton findCustomerButton;
    private javax.swing.JLabel findCustomerLabel;
    private javax.swing.JTextField findCustomerTF;
    private javax.swing.JButton findEmployeeButton;
    private javax.swing.JLabel findEmployeeLabel;
    private javax.swing.JTextField findEmployeeTF;
    private javax.swing.JLabel findFromDateLabel;
    private javax.swing.JLabel findMaximumPriceLabel;
    private javax.swing.JSpinner findMaximumPriceSP;
    private javax.swing.JLabel findMinimumPriceLabel;
    private javax.swing.JSpinner findMinimumPriceSP;
    private javax.swing.JLabel findPaidLabel;
    private javax.swing.JRadioButton findPaidNoRB;
    private javax.swing.JRadioButton findPaidYesRB;
    private javax.swing.JTextField findSaleDateFromTF;
    private javax.swing.JLabel findSaleDateLabel;
    private javax.swing.JTextField findSaleDateToTF;
    private javax.swing.JLabel findSaleLabel;
    private javax.swing.JPanel findSalePanel;
    private javax.swing.JButton findShowAllButton;
    private javax.swing.JTable findTable;
    private javax.swing.JLabel findToDateLabel;
    private javax.swing.JLabel getIdNoLabel;
    private javax.swing.JSpinner getIdSP;
    private javax.swing.JButton getSaleIdButton;
    private javax.swing.JLabel getSaleLabel;
    private javax.swing.JPanel getSalePanel;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JScrollPane jScrollPane3;
    private javax.swing.JTabbedPane jTabbedPane1;
    private javax.swing.JTable jTable1;
    private javax.swing.JButton newSaleButton;
    private javax.swing.ButtonGroup payedBG;
    // End of variables declaration//GEN-END:variables
}